﻿using Roslyn.Compilers.CSharp;

namespace InterfaceWeaver.CustomTool
{
    public static class LiteralExpressionSyntaxExtensions
    {
        /// <summary>
        /// Compiles the value.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static object CompileValue(this LiteralExpressionSyntax expression)
        {
            var compilation = Compilation.Create("LiteralValueCompilation", syntaxTrees: new[] { expression.SyntaxTree });
            var semanticModel = compilation.GetSemanticModel(expression.SyntaxTree);
            var value = semanticModel.GetConstantValue(expression);
            return value.Value;
        }
    }
}
